package edu.scau.pos.service;

import edu.scau.core.Enum.OrderStatus;
import edu.scau.core.Enum.SaleLineItemStatus;
import edu.scau.core.exection.BusinessProcessException;
import edu.scau.pos.domain.Orders;
import edu.scau.pos.domain.SaleLineItem;
import edu.scau.pos.vo.OrdersVo;
import edu.scau.sku.service.SkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class OrderManageServiceImpl implements OrderManageService {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private SkuService skuService;
    @Autowired
    private SaleLineItemService saleLineItemService;
    @Autowired
    private ProductService productService;
    /**
     * 缺货补发
     * @param ordersVo
     * @return
     */
    @Override
    public Map backorderShipment(OrdersVo ordersVo) {
        Orders orders=ordersService.findByOrderId(ordersVo.getOrderId());
        HashMap<String,Object> data=new HashMap<>();
        this.checkIfRefund(orders);
        //saleLineItem-->productSn-->sknCode
        Map<String,Integer> skuMap =this.getSkuCodeListFromOrders(orders.getSaleLineItems());
        boolean support = skuService.isStockSufficient(skuMap);
        if(!support){
            data.put("msg","库存余量不足");
            return data;
        }
        //
        skuService.batchUpdateStock(skuMap);
        this.updateOrderMsg(orders);
        data.put("msg","补发成功");
        return data;
    }

    @Override
    public Map backorderShipmentByOrderId(String ordersId) {
       OrdersVo ordersVo=ordersService.findOrdersByOrderId(ordersId);
       return this.backorderShipment(ordersVo);
    }

    /**
     * 出库后更新order状态
     * @param orders
     */
    private void updateOrderMsg(Orders orders) {
        orders.setStatus(OrderStatus.Paid.getSignal());
        List<SaleLineItem> saleLineItems = orders.getSaleLineItems();
        for (SaleLineItem saleLineItem : saleLineItems) {
            saleLineItem.setStatus(SaleLineItemStatus.Dispatch.getSignal());
        }
        int r1= saleLineItemService.updateBatch(saleLineItems);
        if(r1!=saleLineItems.size()){
            log.info("缺货补发条目状态更新失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        int r2=ordersService.updateOrders(orders);
        if(r2<=0){
            log.info("缺货补发更新订单状态失败");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
    }

    private Map<String, Integer> getSkuCodeListFromOrders(List<SaleLineItem> saleLineItems) {
        Map<String,Integer> skuMap=new HashMap<>();
        for (SaleLineItem saleLineItem : saleLineItems) {
            skuMap.put(productService.getSkuCodeFromProductSn(saleLineItem.getProductSn()),saleLineItem.getQuantity());
        }
        return skuMap;
    }

    private void checkIfRefund(Orders orders) {
        if(orders.getStatus()== OrderStatus.Refunded.getSignal()){
            throw new BusinessProcessException("订单已取消");
        }
        if(orders.getStatus()!= OrderStatus.Backorder.getSignal()){
            throw new BusinessProcessException("订单已发货,不可重复发货");
        }
    }

}
